<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
global $dal;

$visibility = Array();

if( $_GET[ 'for' ] == 'faction' ) {
	$statement = $dal->prepare( 'SELECT faction_id ' .
			'FROM faction_member ' .
			'WHERE ruler_id = :ruler_id AND invited = 0 AND requested = 0' );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$factionId = $dal->getValue( $statement );
	if( isset( $factionId ) ) {
		$statement = $dal->prepare( 'SELECT actual_x, actual_y, scouting ' .
				'FROM army ' .
				'JOIN officers ON army.officer_id = officers.officer_id ' .
				'JOIN faction_member ON army.ruler_id = faction_member.ruler_id ' .
				'WHERE faction_id = :faction_id AND scout + infantry + cavalry + artillery > 0' );
		$statement->bindParam( ':faction_id', $factionId, PDO::PARAM_INT );
		$armies = $dal->getArrayOfObjects( $statement );
		foreach( $armies AS $army ) {
			$visibility = addVisibilityRange( $visibility, round( $army->actual_x ), round( $army->actual_y ), 3 + $army->scouting );
		}
	
		$statement = $dal->prepare( 'SELECT x, y, building_id FROM village ' .
						'JOIN village_buildings ON village.village_id = village_buildings.village_id ' .
						'JOIN faction_member ON village.ruler_id = faction_member.ruler_id ' .
						'WHERE faction_id = :faction_id ' .
								'AND ( ( building_id = 21 AND ' .
									'( SELECT count(*) FROM village_buildings AS second_buildings WHERE second_buildings.village_id = village_buildings.village_id AND second_buildings.building_id = 25 ) = 0 ) ' .
								'OR building_id = 25 )' );
		$statement->bindParam( ':faction_id', $factionId, PDO::PARAM_INT );
		$villages = $dal->getArrayOfObjects( $statement );
		foreach( $villages AS $village ) {
			$visibility = addVisibilityRange( $visibility, $village->x, $village->y, $village->building_id == 25 ? 20 : 8 );
		}
	}
} 
elseif( $_GET[ 'for' ] == 'ruler' ) {
	$statement = $dal->prepare( 'SELECT actual_x, actual_y, scouting ' .
			'FROM army ' .
			'JOIN officers ON army.officer_id = officers.officer_id ' .
			'WHERE army.ruler_id = :ruler_id AND scout + infantry + cavalry + artillery > 0' );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$armies = $dal->getArrayOfObjects( $statement );
	foreach( $armies AS $army ) {
		$visibility = addVisibilityRange( $visibility, round( $army->actual_x ), round( $army->actual_y ), 3 + $army->scouting );
	}

	$statement = $dal->prepare( 'SELECT x, y, building_id FROM village ' .
					'JOIN village_buildings ON village.village_id = village_buildings.village_id ' .
					'WHERE ruler_id = :ruler_id ' .
							'AND ( ( building_id = 21 AND ' .
								'( SELECT count(*) FROM village_buildings AS second_buildings WHERE second_buildings.village_id = village_buildings.village_id AND second_buildings.building_id = 25 ) = 0 ) ' .
							'OR building_id = 25 )' );
	$statement->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$villages = $dal->getArrayOfObjects( $statement );
	foreach( $villages AS $village ) {
		$visibility = addVisibilityRange( $visibility, $village->x, $village->y, $village->building_id == 25 ? 20 : 8 );
	}
}

print( json_encode( $visibility ) );

function addVisibilityRange( $visibility, $x, $y, $range ) {
	$visibilityRange = new stdClass();
	$visibilityRange->range = $range;
	$visibilityRange->x = $x;
	$visibilityRange->y = $y;
	$visibility[] = $visibilityRange;
	return $visibility;
}
?>
